// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIMarketFieldsCache.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.UI.Forms
{
    #region Using Directives

    using System;

    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.UI.Forms;

    #endregion

    /// <summary>
    /// The ui instrument.
    /// </summary>
    public partial class UIMarketFieldsCache : UIControl
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIMarketFieldsCache"/> class. 
        /// Initializes a new instance of the <see cref="UIMarketFields"/> class.
        /// </summary>
        public UIMarketFieldsCache()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIMarketFieldsCache"/> class. 
        /// Initializes a new instance of the <see cref="UIMarketFields"/> class.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        public UIMarketFieldsCache(IInstrument i)
        {
            this.Instrument = i;
            this.InitializeComponent();
        }

        #endregion

        #region Properties

        /// <summary>
        /// The instrument.
        /// </summary>
        public IInstrument Instrument { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// The format cell.
        /// </summary>
        /// <param name="cell">
        /// The cell.
        /// </param>
        /// <returns>
        /// The format cell.
        /// </returns>
        private string FormatCell(MarketField cell)
        {
            if (cell == null || ! cell.HasValue)
            {
                return "-";
            }

            var s = string.Empty;
            if (cell.IsDateTime)
            {
                s = PrettyPrinter.Timings.Format(cell);
            }
            else
            {
                s = string.Empty + cell.Value;
            }

            return s;
        }

        /// <summary>
        /// The markefieldscacheGrid_ ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnMarkefieldscacheGridUIGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
            e.RowCount = Enum.GetValues(typeof(EMarketField)).Length;
        }

        /// <summary>
        /// The markefieldscache ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnMarkefieldscacheUIGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

            // According the column display required data
            switch (e.ColumnIndex)
            {
                case 0:
                    e.Value = (EMarketField)e.RowIndex;
                    break;

                case 1:
                    {
                        var cell = this.Instrument.MarketFields[(EMarketField)e.RowIndex];
                        e.Value = this.FormatCell(cell);
                    }

                    break;
            }
        }

        #endregion
    }
}